<% layout('/layouts/default.html', {title: '推广项管理', libs: ['validate','fileupload','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<#form:form id="inputForm" model="${plateItem}" action="${ctx}/storage/plate/item/save" method="post" class="form-horizontal">
		<div class="box-body">
			<div class="form-unit">${text('基本信息')}</div>
			<#form:hidden path="id"/>
			<#form:hidden path="plateId"/>
			<div class="row">
				<div class="col-xs-12">
					<div class="form-group">
						<label class="control-label col-sm-2">${text('缩略图')}：</label>
						<div class="col-sm-10">
							<#form:hidden class="form-control" id="thumbnail" name="thumbnail" path="thumbnail"/>
							<#form:fileupload id="imagethumbnail" uploadType="image" returnPath="true" filePathInputId="thumbnail" class="form-control" readonly="false" maxUploadNum="1"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required ">*</span> ${text('顺序')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="sort" maxlength="255" class="form-control required digits"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required ">*</span> ${text('类型')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<% var types = [{type:'活动'},{type:'栏目'},{type:'商品'}]; %>
							<#form:select path="type" class="form-control" items="${types}" itemLabel="type" itemValue="type" blankOption="true" blankOptionLabel="请选择"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-12">
					<div class="form-group">
						<label class="control-label col-sm-2" title="">
							<span class="required hide">*</span> ${text('营销活动')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-10">
							<#form:listselect path="activityId" labelPath="activity.name" title="活动选择" url="${ctx}/storage/activity/select" allowClear="true" checkbox="false" itemCode="id" itemName="name"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-12">
					<div class="form-group">
						<label class="control-label col-sm-2" title="">
							<span class="required hide">*</span> ${text('栏目')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-10">
							<#form:listselect path="routeId" labelPath="route.name" title="栏目选择" url="${ctx}/storage/route/select" allowClear="true" checkbox="false" itemCode="id" itemName="name"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-12">
					<div class="form-group">
						<label class="control-label col-sm-2" title="">
							<span class="required hide">*</span> ${text('商品')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-10">
							<#form:listselect path="goodsId" labelPath="goods.name" title="商品选择" url="${ctx}/storage/goods/select" allowClear="true" checkbox="false" itemCode="id" itemName="name"/>
						</div>
					</div>
				</div>
			</div>
			<h4 class="form-unit">${text('商品表')}</h4>
			<div class="ml10 mr10">
				<table id="goodsDataGrid"></table>
				<a href="#" id="linkGoodsDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
			</div>
		</div>
		<div class="box-footer">
			<div class="row">
				<div class="col-sm-offset-2 col-sm-10">
					<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
					<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
				</div>
			</div>
		</div>
	</#form:form>
</div>
</div>
<% } %>
<script>
	function chanageType(){
		$("[name='activityId']").parents(".row").hide();
		$("[name='routeId']").parents(".row").hide();
		$("[name='goodsId']").parents(".row").hide();
		switch ($("#type option:selected").val()) {
			case "活动":
				$("[name='activityId']").parents(".row").show();
				break;
			case "栏目":
				$("[name='routeId']").parents(".row").show();
				break;
			case "商品":
				$("[name='goodsId']").parents(".row").show();
				break;
		}
	}
	$(function () {
		$(document).on("change","#type",function () {
			chanageType();
		});
		chanageType();
	});
</script>

<script>
	$('#linkGoodsDataGridAddRowBtn').click(function(){
		var selectData = [],
				boxWidth = $(top.window).width() - 100,
				boxHeight = $(top.window).height() - 100;
		boxWidth = boxWidth < 350 ? 350 : boxWidth;
		boxHeight = boxHeight < 250 ? 250 : boxHeight;
		var options = {
			type: 2,
			maxmin: true,
			shadeClose: true,
			title: '选择商品',
			area: [boxWidth+'px', boxHeight+'px'],
			content: "${ctx}/storage/goods/select?checkbox=true",
			contentFormData: {
				selectData: js.encodeUrl(JSON.stringify(selectData))
			},
			success: function(layero, index){
				if ($(js.layer.window).width() < boxWidth || $(js.layer.window).height() < boxHeight){
					js.layer.full(index);
				}
			},
			btn: ['<i class="fa fa-check"></i> 确定'],
			btn1: function(index, layero){
				var win = layero.iframeWindow();
				selectData = win.getSelectData();
				console.log(selectData);
				var ids = jQuery("#goodsDataGrid").jqGrid("getDataIDs");
				//获得当前最大行号（数据编号）
				var maxRowId = Math.max.apply(Math,ids);
				//获得新添加行的行号（数据编号）
				var newRowId = maxRowId;

				$.each(selectData,function (goodsIndex,goods) {
					var data = {
						goodsId:goods.id,
						goods:goods
					};
					newRowId = maxRowId + 1;
					$("#goodsDataGrid").jqGrid('addRowData', 0, data, "last");
					$('#goodsDataGrid').jqGrid('editRow', 0, false);
				});
			}
		};
		options.btn.push('<i class="fa fa-close"></i> 关闭');
		js.layer.open(options);
	});

	//初始化活动商品表DataGrid对象
	$("#goodsDataGrid").dataGrid({
		data: ${toJson(plateItem.goodsList)},
		datatype: "local", // 设置本地数据
		autoGridHeight: function(){return 'auto'}, // 设置自动高度

		// 设置数据表格列
		columnModel: [
			{header:'${text("商品id")}', name:'goodsId', editable:true, hidden:true},
			{header:'${text("状态")}', name:'status', editable:true, hidden:true},
			{header:'${text("商品主图")}', name:'goods.thumbnail', index:'a.goods.thumbnail', width:110, align:"center",formatter: function(val, obj, row, act){
				return '<img src="' + val + '" style="width: 80px;height: 80px"/>';
			}},
			{header:'${text("编码")}', name:'goods.code', index:'a.goods.code', width:150},
			{header:'${text("名称")}', name:'goods.name', index:'a.goods.name', width:150},
			{header:'${text("吊牌价")}', name:'goods.price', index:'a.goods.price', width:150, align:"center",formatter: function(val, obj, row, act){
					return "￥" + val;
				}},
			{header:'${text("普通会员价")}', name:'goods.memberPrice', index:'a.goods.memberPrice', width:150, align:"center",formatter: function(val, obj, row, act){
					return "￥" + val;
				}},
			{header:'${text("VIP会员价")}', name:'goods.vipPrice', index:'a.goods.vipPrice', width:150, align:"center",formatter: function(val, obj, row, act){
					return "￥" + val;
				}},
			{header:'${text("代购费")}', name:'goods.agentPrice', index:'a.goods.agentPrice', width:150, align:"center",formatter: function(val, obj, row, act){
					return "￥" + val;
				}},
			{header:'${text("库存")}', name:'goods.stockNum', width:150, align:"center"},
			{header:'${text("操作")}', name:'actions', width:160, align:"center", sortable:false, title:false, formatter: function(val, obj, row, act){
				var actions = [];
				if (val == 'new'){
					actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#goodsDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
				}else{
					actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#goodsDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
				}
				return actions.join('');
			}}
		],

		// 编辑表格参数
		editGrid: true,				// 是否是编辑表格
		editGridInitRowNum: 0,		// 编辑表格的初始化新增行数
		editGridAddRowInitData: {id: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据

		// 编辑表格的提交数据参数
		editGridInputFormListName: 'goodsList', // 提交的数据列表名
		editGridInputFormListAttrs: 'status,goodsId' // 提交数据列表的属性字段
	});
</script>
<script>
	$("#inputForm").validate({
		submitHandler: function(form){
			js.ajaxSubmitForm($(form), function(data){
				js.showMessage(data.message);
				if(data.result == Global.TRUE){
					js.closeCurrentTabPage(function(contentWindow){
						contentWindow.page();
					});
				}
			}, "json");
		}
	});
</script>